package org.colin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.service.impl.FastDFSServiceImpl;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(tags = "文件上传")
@RequestMapping("/fdfs")
public class FileController {


    @Autowired
    FastDFSServiceImpl fastDFSService;

    @ApiOperation(value = "文件上传")
    @LogAnnotation(title = "基础数据管理", action = "文件上传")
    @ResponseBody
    @PostMapping(value = "uploadFile")//,@RequestParam("type") Integer type,@RequestParam("statusv") Integer statusv
    public DataResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        DataResult success = DataResult.success();
        if(file != null) {
            String filename = file.getOriginalFilename();
            if (StringUtils.isNotBlank(filename)) {
                String[] split = filename.split("\\.");
                String suffix = split[split.length-1];
                String upload = fastDFSService.upload(file, suffix);
                map.put("path",upload);
            }
            map.put("fileName",filename);
            success.setData(map);
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "上传失败，文件不能为空");
        }
        return success;
    }



    @ApiOperation(value = "批量文件上传")
    @LogAnnotation(title = "基础数据管理", action = "批量文件上传")
    @ResponseBody
    @PostMapping(value = "uploadFiles")
    public DataResult uploadFiles(@RequestParam("files") MultipartFile[] files) throws Exception {
        DataResult success = DataResult.success();
        ArrayList arrayList = new ArrayList();
        if(files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                HashMap<String, String> map = new HashMap<>();
                MultipartFile file = files[i];
                String filename = file.getOriginalFilename();
                if (StringUtils.isNotBlank(filename)) {
                    String[] split = filename.split("\\.");
                    String suffix = split[split.length-1];
                    String upload = fastDFSService.upload(file, suffix);
                    map.put("path",upload);
                }
                map.put("fileName",filename);
                arrayList.add(map);
            }
            success.setData(arrayList);
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "上传失败，文件不能为空");
        }

        return success;
    }


}
